package com.yuwj.chinaa.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yuwj.chinaa.R;
import com.yuwj.chinaa.activity.AssetsAvailableMoneyAvtivity;
import com.yuwj.chinaa.activity.AssetsFenqFreezeMoneyAvtivity;
import com.yuwj.chinaa.activity.AssetsFreezeMoneyAvtivity;
import com.yuwj.chinaa.activity.AssetsReceiveActivity;
import com.yuwj.chinaa.activity.AssetsTransferActivity;
import com.yuwj.chinaa.activity.MyCommunityActivity;
import com.yuwj.chinaa.activity.OrderBaseListActivity;
import com.yuwj.chinaa.base.BaseLazyLoadFragment;
import com.yuwj.chinaa.entity.EntityBurseCoinNumber;
import com.yuwj.chinaa.entity.EntityOtherAssets;
import com.yuwj.chinaa.presenter.IPresenter;
import com.yuwj.chinaa.presenter.presenterImpl.PresenterImpl;
import com.yuwj.chinaa.util.ToastUtil;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class FragmentAssets extends BaseLazyLoadFragment {
    @BindView(R.id.tv_assets_chinaa)
    TextView tvAssetsChinaA;
    @BindView(R.id.tv_assets_other)
    TextView tvAssetsOther;
    @BindView(R.id.tv_assets_chinaa_acount)
    TextView tvAssetsChinaAAcount;

    @BindView(R.id.tv_available_coin)
    TextView tvAvailableCoin;
    @BindView(R.id.tv_freeze_coin)
    TextView tvFreezeCoin;
    @BindView(R.id.tv_fenq_freeze_coin)
    TextView tvFenqFreezeCoin;
    @BindView(R.id.tv_share_coin)
    TextView tvShareCoin;
    @BindView(R.id.tv_generalize_coin)
    TextView tvGeneralizeCoin;
    @BindView(R.id.ll_assets_receive)
    LinearLayout llAssetsReceive;
    @BindView(R.id.ll_assets_transfer)
    LinearLayout llAssetsTransfer;
    @BindView(R.id.ll_assets_trade_order)
    LinearLayout llAssetsTradeOrder;
    @BindView(R.id.ll_assets_community)
    LinearLayout llAssetsCommunity;
    @BindView(R.id.ll_assets_available_money)
    LinearLayout llAssetsAvailableMoney;
    @BindView(R.id.ll_assets_freeze_money)
    LinearLayout llAssetsFreezeMoney;
    @BindView(R.id.ll_assets_fenq_freeze_money)
    LinearLayout llAssetsFenqFreezeMoney;
    @BindView(R.id.tv_btcusd)
    TextView tvBtcusd;
    @BindView(R.id.tv_btccny)
    TextView tvBtccny;
    @BindView(R.id.tv_ethusd)
    TextView tvEthusd;
    @BindView(R.id.tv_ethcny)
    TextView tvEthcny;
    @BindView(R.id.ll_other_assets_container)
    LinearLayout llOtherAssetsContainer;
    @BindView(R.id.ll_assets_container)
    LinearLayout llAssetsContainer;
    Unbinder unbinder;


    private int assetsType;//资产类型，0-ChinaA,1-其他
    IPresenter presenter;
    EntityBurseCoinNumber entityBurseCoinNumber;//各种钱包币的数量实体类
    public boolean isLoadSuccess;

    public static FragmentAssets createInstance() {
        FragmentAssets fragmentAssets = new FragmentAssets();
        Bundle bundle = new Bundle();
        fragmentAssets.setArguments(bundle);
        return fragmentAssets;
    }

    @Override
    public int setCustomContentViewResourceId() {
        return R.layout.fragment_assets;
    }

    @Override
    protected void lazyLoad() {
        super.lazyLoad();
        initParam();
        initView();
        initOperator();
        isLoadSuccess = true;
    }


    public void initParam() {
        presenter = new PresenterImpl(this);
    }

    public void initView() {
        switch (assetsType) {
            case 0:
                llAssetsContainer.setVisibility(View.VISIBLE);
                llOtherAssetsContainer.setVisibility(View.GONE);
                break;
            case 1:
                llAssetsContainer.setVisibility(View.GONE);
                llOtherAssetsContainer.setVisibility(View.VISIBLE);
                break;
        }
    }

    public void initOperator() {
        refreshUserBurseNumberList();
        JSONObject jsonObjectWith1 = new JSONObject();
        try {
            jsonObjectWith1.put("type", "btc_usdt");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        presenter.otherAsset(jsonObjectWith1.toString());
        JSONObject jsonObjectWith2 = new JSONObject();
        try {
            jsonObjectWith2.put("type", "btc_qc");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        presenter.otherAsset(jsonObjectWith2.toString());
        JSONObject jsonObjectWith3 = new JSONObject();
        try {
            jsonObjectWith3.put("type", "eth_usdt");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        presenter.otherAsset(jsonObjectWith3.toString());
        JSONObject jsonObjectWith4 = new JSONObject();
        try {
            jsonObjectWith4.put("type", "eth_qc");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        presenter.otherAsset(jsonObjectWith4.toString());
    }

    public void refreshUserBurseNumberList() {
        JSONObject param = new JSONObject();
        presenter.getUserBurseCoinNumberList(param.toString());
    }

    public void updateBurseCoinNumberView() {
        tvAvailableCoin.setText(entityBurseCoinNumber.getAvailableBurseCoinNumber() + "");
        tvFreezeCoin.setText(entityBurseCoinNumber.getFreezeBurseCoinNumber() + "");
        tvFenqFreezeCoin.setText(entityBurseCoinNumber.getStagingFreezeBurseCoinNumber() + "");
        tvShareCoin.setText(entityBurseCoinNumber.getShareCoinNumber() + "");
        tvGeneralizeCoin.setText(entityBurseCoinNumber.getGeneralizeCoinNumber() + "");
        tvAssetsChinaAAcount.setText(entityBurseCoinNumber.getAvailableBurseCoinNumber() + entityBurseCoinNumber.getFreezeBurseCoinNumber()
                + entityBurseCoinNumber.getStagingFreezeBurseCoinNumber() + "  china-A");


    }


    @OnClick({R.id.tv_assets_chinaa, R.id.tv_assets_other, R.id.ll_assets_receive, R.id.ll_assets_transfer, R.id.ll_assets_trade_order,
            R.id.ll_assets_community, R.id.ll_assets_available_money, R.id.ll_assets_freeze_money, R.id.ll_assets_fenq_freeze_money})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_assets_chinaa:
                tvAssetsChinaA.setTextColor(getResources().getColor(R.color.red));
                tvAssetsChinaA.setBackgroundResource(R.mipmap.ic_user_type_select_bg);
                tvAssetsOther.setTextColor(getResources().getColor(R.color.white));
                tvAssetsOther.setBackgroundColor(Color.argb(0, 0, 0, 0));
                assetsType = 0;
                initView();
                break;
            case R.id.tv_assets_other:
                tvAssetsOther.setTextColor(getResources().getColor(R.color.red));
                tvAssetsOther.setBackgroundResource(R.mipmap.ic_user_type_select_bg);
                tvAssetsChinaA.setTextColor(getResources().getColor(R.color.white));
                tvAssetsChinaA.setBackgroundColor(Color.argb(0, 0, 0, 0));
                assetsType = 1;
                initView();
                break;
            case R.id.ll_assets_receive:
                startActivity(AssetsReceiveActivity.createIntent(context));
                break;
            case R.id.ll_assets_transfer:
                startActivity(AssetsTransferActivity.createIntent(context));
                break;
            case R.id.ll_assets_trade_order:
                startActivity(OrderBaseListActivity.createIntent(context));
                break;
            case R.id.ll_assets_community:
                startActivity(MyCommunityActivity.createIntent(context));
                break;
            case R.id.ll_assets_available_money:
                startActivity(AssetsAvailableMoneyAvtivity.createIntent(context));
                break;
            case R.id.ll_assets_freeze_money:
                startActivity(AssetsFreezeMoneyAvtivity.createIntent(context));
                break;
            case R.id.ll_assets_fenq_freeze_money:
                startActivity(AssetsFenqFreezeMoneyAvtivity.createIntent(context));
                break;

        }

    }


    @Override
    public void onGetUserBurseCoinNumberListSuccess(EntityBurseCoinNumber entityBurseCoinNumber) {
        super.onGetUserBurseCoinNumberListSuccess(entityBurseCoinNumber);
        this.entityBurseCoinNumber = entityBurseCoinNumber;
        updateBurseCoinNumberView();
    }

    @Override
    public void onGetUserBurseCoinNumberListFail(Exception e) {
        super.onGetUserBurseCoinNumberListFail(e);
        ToastUtil.getInstance().shortLength(context, e.getMessage());
    }

    @Override
    public void onOtherAssetSuccess(String type, EntityOtherAssets entityOtherAssets) {
        super.onOtherAssetSuccess(type, entityOtherAssets);
        switch (type) {
            case "btc_usdt":
                tvBtcusd.setText(entityOtherAssets.getLast() + "");
                break;
            case "btc_qc":
                tvBtccny.setText(entityOtherAssets.getLast() + "");
                break;
            case "eth_usdt":
                tvEthusd.setText(entityOtherAssets.getLast() + "");
                break;
            case "eth_qc":
                tvEthcny.setText(entityOtherAssets.getLast() + "");
                break;
        }
    }

    @Override
    public void onOtherAssetFail(Exception e) {
        super.onOtherAssetFail(e);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
